package live.tag;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import live.common.Format;

import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;


public class WeekTab extends TagSupport {
	private Format format = Format.getInstance();
	/**
	 * 
	 */
	private static final long serialVersionUID = 5464705147483063444L;

	private String timeStr;
	private String strHref;

	public String getTimeStr() {
		return timeStr.trim();
	}

	public void setTimeStr(String timeStr) throws JspException {
		this.timeStr = (String) ExpressionEvaluatorManager.evaluate("timeStr",
				timeStr, Object.class, this, this.pageContext);
	}

	public String getStrHref() {
		return strHref;
	}

	public void setStrHref(String strHref) throws JspException {
		this.strHref = (String) ExpressionEvaluatorManager.evaluate("strHref",
				strHref,Object.class, this, this.pageContext);
	}

	@Override
	public int doEndTag() throws JspException {
		// TODO Auto-generated method stub
		try {
		Date today = new Date();
		Calendar calendarToday = new GregorianCalendar();
		calendarToday.setTime(today);
		Date inDate=format.stringToDate(timeStr, "yyyy-MM-dd");
		Calendar calendarInDate = new GregorianCalendar();
		calendarInDate.setTime(inDate);
		int today_of_Year=calendarToday.get(calendarToday.DAY_OF_YEAR);
		int inDate_of_Year=calendarInDate.get(calendarInDate.DAY_OF_YEAR);
		int gap=inDate_of_Year-today_of_Year;
		if((inDate_of_Year-today_of_Year)>3){gap=3;}
		if((inDate_of_Year-today_of_Year)<0){inDate=new Date();gap=0;timeStr=format.dateFormatString(inDate, "yyyy-MM-dd");}
		StringBuffer showBuffer = new StringBuffer();
		for (int i = gap; i >= 0; i--) {
			calendarInDate = new GregorianCalendar();
			calendarInDate.setTime(inDate);
			calendarInDate.add(calendarInDate.DAY_OF_WEEK, -i);
			showBuffer.append("<li");
			if(timeStr.equals(format.dateFormatString(calendarInDate.getTime(), "yyyy-MM-dd").trim())){
				showBuffer.append(" class=\"current\"");
			}
			//strHref 最后参数为时间参数,组装
			showBuffer.append("><a href=\"").append(strHref).append(format.dateFormatString(calendarInDate.getTime(), "yyyy-MM-dd")).append("\">")
			.append(format.dateFormatString(calendarInDate.getTime(), "MM-dd")).append("<BR/>").append(returnWeekDay(calendarInDate.get(calendarInDate.DAY_OF_WEEK))).append("</a></li>\n");
			calendarInDate=null;
		}
		for (int j = 0; j < 6-gap; j++) {
			calendarInDate = new GregorianCalendar();
			calendarInDate.setTime(inDate);
			calendarInDate.add(calendarInDate.DAY_OF_WEEK, +j+1);
			showBuffer.append("<li");
			if(timeStr.equals(format.dateFormatString(calendarInDate.getTime(), "yyyy-MM-dd").trim())){
				showBuffer.append(" class=\"current\"");
			}
			showBuffer.append("><a href=\"").append(strHref).append(format.dateFormatString(calendarInDate.getTime(), "yyyy-MM-dd")).append("\">")
			.append(format.dateFormatString(calendarInDate.getTime(), "MM-dd")).append("<BR/>").append(returnWeekDay(calendarInDate.get(calendarInDate.DAY_OF_WEEK))).append("</a></li>\n");
		}
		calendarInDate=null;inDate=null;calendarToday=null;today=null;
			pageContext.getOut().write(showBuffer.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return super.doEndTag();
	}

	private String returnWeekDay(int day_of_week) {
		String day_str = "";
		switch (day_of_week) {
		case 1:
			day_str = "星期日";
			break;
		case 2:
			day_str = "星期一";
			break;
		case 3:
			day_str = "星期二";
			break;
		case 4:
			day_str = "星期三";
			break;
		case 5:
			day_str = "星期四";
			break;
		case 6:
			day_str = "星期五";
			break;
		case 7:
			day_str = "星期六";
			break;
		}
		return day_str;
	}

}
